$(function () {

  let save = $('#save');
  let city = $('#city');
  let areaAdd = $('#area');

  proDate()
    .then(res => {
      console.log(res);
      let dataList = res.list;

      let html = ' <option value="请选择" class="">请选择</option>';
      dataList.forEach(v => {
        const { name } = v;
        html += `
      <option value="${name}">${name}</option>`
      });
      save.html(html);

      //  1.save发生改变
      let saves = [];
      save.on('change', function () {
        city.show();
        let province = save.val();
        if (province === '请选择') {
          city.html('<option value="请选择">请选择</option>');
          areaAdd.html('<option value="请选择">请选择</option>');
          return;
        }

        // areaAdd.innerHTML = '<option value="请选择">请选择</option>';
        saves = dataList.filter(v => v.name === province)[0].list;
        console.log(saves);
        let arr = ' <option value="请选择">请选择</option>';
        saves.forEach(v => {
          const { name } = v;
          arr += `
        <option value="${name}">${name}</option>
        `
        });
        city.html(arr);

      })
      // 2.当城市发生改变
      city.on('change', function () {
        areaAdd.html("");
        // areaAdd.show();
        let cityS = city.val();
        let area = saves.filter(v => v.name === cityS)[0].list
        if (cityS === '请选择') {
          areaAdd.html('<option value="" >请选择</option>')
          return;
        }

        if (area === undefined) {
          areaAdd.hide()
        } else {
          areaAdd.show()
          let areaSum = '<option value="" >请选择</option>';
          area.forEach(v => {
            const { name } = v;
            // console.log(name);
            areaSum += `
        <option value="${name}">${name}</option>
        `
          })
          areaAdd.html(areaSum);
        }
      })
    })


  const admit = localStorage.getItem('someone');
  const hello = $('.hello');
  const Admit = $('.navAdmit');
  const Reg = $('.navReg');
  // 1.1获取谁在登录
  // admit ? hello.html(admit) : hello.html('考拉欢迎你');
  // 1.3处理登录后事件(显示登录名等)
  if (admit) {
    hello.html(admit);
    Admit.hide();
    Reg.hide();
  }


  //2. 获取你的收货地址
  const pay = $('.pay');
  pay.click(function () {
    let moneyPay = localStorage.getItem('money');
    let saveA = $("#save  option:selected").text();
    let cityA = $('#city   option:selected').text();
    let areaA = $('#area   option:selected').text();
    let deA = $('.detail').val();
    let youA = $('.name').val();

    console.log(saveA !== '请选择');
    if (saveA !== '请选择' && cityA !== '请选择' && areaA !== '请选择' && deA && youA) {
      $('.right').show();
      $('.r-title').html(`${youA}一共消费${moneyPay}元<br>你的收货地址是：
      ${saveA}省${cityA}市${areaA}区${deA}
      `)
    }



    // console.log(areaA == '');
    // console.log(saveA, cityA, deA, areaA, youA);


  })














})